/**
 * <p>Title: IllWordFilterImplLocal.java</p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2009</p>
 * <p>Company: xunjie</p>
 * @author shenhao
 * @date 2009-12-31
 * @version 1.0
 */
package cn.haona.game.simb.comm.util;

import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Vector;

/**
 * <p>
 * Title: IllWordFilterImplLocal
 * </p>
 * <p>
 * Description: 敏感信息过滤器
 * </p>
 * <p>
 * Company: xunjie
 * </p>
 * 
 * @author shenhao
 * @date 2009-12-31
 */
public class IllWordFilterImplLocal implements IllWordFilter {

	private static String[] illwords;

	private static final String spt = ",";

	/**
	 * 
	 */
	public IllWordFilterImplLocal(String fileName) {
		// 返回的数组
		System.out.println(fileName);
		try {
			// 输入流
			InputStream is = new FileInputStream(fileName);
			int size = is.available();
			// 文件byte[]
			byte[] arrbFile = new byte[size];
			is.read(arrbFile);
			is.close();
			// 关键字的字符串
			String strIllWord = new String(arrbFile);
			// 切割字符串
			Vector vBuffer = new Vector();

			// 当前的索引
			int iCurIndex = 0;
			// 上次的索引
			int iPreIndex = 0;
			String strTemp;
			while ((iCurIndex = strIllWord.indexOf(spt, iCurIndex)) != -1) {
				// 取出关键字
				strTemp = strIllWord.substring(iPreIndex, iCurIndex);
				// 滤掉两边的空格
				strTemp.trim();
				// 加入数组
				vBuffer.addElement(strTemp);

				if (iCurIndex + spt.length() < strIllWord.length()) {
					iCurIndex += spt.length();
				}
				iPreIndex = iCurIndex;
			}

			// 最后一个没有‘分隔号’
			int iLength = strIllWord.length();
			strTemp = strIllWord.substring(iPreIndex, iLength);
			strTemp.trim();
			// 加入数组
			vBuffer.addElement(strTemp);

			// 初始化返回数组
			illwords = new String[vBuffer.size()];
			for (int i = 0; i < illwords.length; i++) {
				illwords[i] = (String) vBuffer.elementAt(i);
			}

		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	/*
	 * (non-Javadoc) <p>Title: checkIllWord</p> <p>Description: </p>
	 * 
	 * @param keyword
	 * 
	 * @return
	 * 
	 * @see
	 * com.xunjie.game.answer.common.util.IllWordFilter#checkIllWord(java.lang
	 * .String)
	 */
	public String checkIllWord(String keyword) {
		String strBack = keyword;
		if (illwords != null) {
			int iLength = illwords.length;
			for (int i = 0; i < iLength; i++) {
				if (keyword.indexOf(illwords[i]) != -1) {
					strBack = null;
				}
			}
		}

		return strBack;
	}

	public boolean isIllWord(String keyword) {
		if (illwords != null) {
			String strBack = keyword;
			int iLength = illwords.length;
			for (int i = 0; i < iLength; i++) {
				if (keyword.indexOf(illwords[i]) != -1) {
					return false;
				}
			}
		}

		return false;
	}

}
